package demo;

//class MyArray {
//    public Object[] objects = new Object[10];
//
//    public Object getPos(int pos) {
//        return this.objects[pos];
//    }
//
//    public void setVal(int pos, Object val) {
//        objects[pos] = val;
//    }
//}

import javax.jws.Oneway;

class MyArray<T> {
    //public Object[] objects = new Object[10]; //最合适
    //public T[] objects = new T[10];
    public T[] objects =(T[]) new Object[10]; //这样写也不好
    public T getPos(int pos) {
        return this.objects[pos];
    }

    public void setVal(int pos, T val) {
        objects[pos] = val;
    }
}


public class Test {
    public static void main(String[] args) {
        //指定的是Integer
        MyArray<Integer> myArray = new MyArray<Integer>();
        myArray.setVal(0,1);
        myArray.setVal(1,10);

        int val = myArray.getPos(1);
        System.out.println(val);
        System.out.println("=======");
        //后面的尖括号里面的包装类可以省略，但是前面的不能省略
        MyArray<String> myArray2 = new MyArray<>();
        myArray2.setVal(0,"hello");
        myArray2.setVal(1,"world");
        String ret = myArray2.getPos(0);
        System.out.println(ret);
    }


    public static void main1(String[] args) {
        MyArray myArray = new MyArray();
        myArray.setVal(0,"121");
        myArray.setVal(1,10);

        int val = (int)myArray.getPos(1);
        System.out.println(val);
    }
}
